package com.ztools.pc.service.impl.sys;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.sys.Dict;
import com.ztools.common.model.pc.sys.DictData;
import com.ztools.common.util.common.PageUtil;
import com.ztools.pc.mapper.sys.DictDataMapper;
import com.ztools.pc.service.sys.DictDataService;
import com.ztools.pc.service.sys.DictService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: seeTools
 * @Date: 2023/5/22  11:01
 * @Version 1.0
 */
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements DictDataService {

    @Lazy
    @Resource
    private DictService dictService;

    @Override
    public Page<DictData> findList(PageUtil<DictData> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<DictData> queryWrapper = getQueryWrapper(pageUtil.getParams());
        Page<DictData> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<DictData> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public List<DictData> findByObject(DictData dictData) {
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>(dictData);
        return list(queryWrapper);
    }

    @Override
    public boolean delete(DictData dictData) {
        boolean b = false;
        DictData byId = getById(dictData);
        b = removeById(dictData);
        return b;
    }

    @Override
    public boolean update(DictData dictData) {
        boolean b = false;
        DictData byId = getById(dictData);
        b = updateById(dictData);
        return b;
    }

    @Override
    public List<DictData> findByTypeCodes(Long dictId, String codes) {
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DictData::getDictId, dictId).in(DictData::getCode, Arrays.asList(codes.split(",")));
        return list(queryWrapper);
    }


    public QueryWrapper<DictData> getQueryWrapper(DictData dict) {
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(dict.getCode())) {
            queryWrapper.lambda().like(DictData::getCode, dict.getCode());
        }
        if (StrUtil.isNotEmpty(dict.getValue())) {
            queryWrapper.lambda().like(DictData::getValue, dict.getValue());
        }
        if (dict.getDictId() != null) {
            queryWrapper.lambda().eq(DictData::getDictId, dict.getDictId());
        }
        return queryWrapper;
    }

    /**
     * 字典翻译
     *
     * @param dictDataList 对象集合
     */
    public void dictionary(List<DictData> dictDataList) {
        for (DictData dictData : dictDataList) {
            dictionary(dictData);
        }
    }

    public void dictionary(DictData dictData) {
        if (dictData != null) {
            if (dictData.getDictId() != null) {
                Dict dict = dictService.getById(dictData.getDictId());
                dictData.setDictName(dict.getName());
            }
        }
    }

}
